<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class AuthController extends Controller
{
    public function scanPermission()
    {

    }

    public function login(Request $request)
    {
        $validator = $request->validate([
            'username' => 'required',
            'password' => 'required',
        ]);
        /** @var AdminUserModel $user */
        $user = (new AdminUserModel)->where('username', $validator['username'])->first();
        if (! $user) {
            return $this->success(1, msg: '用户不存在');
        }
        if (! $user->checkPassword($validator['password'])) {
            return $this->success(1, msg: '密码错误');
        }
        Auth::login($user, true);
        //todo 判断权限md5是否有改变，有改变则更新更新

        $result = [
            'userId' => $user->id,
            'token' => '123',
        ];

        return $this->success(data: $result);
    }
}
